package cn.webrx;

import java.util.Arrays;
import java.util.Random;

public class MySort {
    public static void main(String[] args) {
        //实现冒泡排序
        Random rand = new Random();
        int[] arr = new int[10];
        System.out.println(Arrays.toString(arr));

        //初始化
        for (int i = 0; i < arr.length; i++) {
            arr[i] = rand.nextInt(1, 101);
        }
        System.out.println(Arrays.toString(arr));

        //冒泡排序
        for (int m = 0; m < arr.length - 1; m++) {
            for (int n = 0; n < arr.length - m - 1; n++) {
                //> 升序 < 降序
                if (arr[n] < arr[n + 1]) {
                    int t = arr[n];
                    arr[n] = arr[n + 1];
                    arr[n + 1] = t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));

        //乱序算法
        for (int i = 0; i < arr.length * arr.length; i++) {
            int a = rand.nextInt(arr.length);
            int b = rand.nextInt(arr.length);
            if (a == b) {
                --i;
                continue;
            } else {
                int t = arr[a];
                arr[a] = arr[b];
                arr[b] = t;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
